package com.uzai.console.service.pyq.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.enums.PlatformTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.feign.turnlink.SimpleTurnLinkDto;
import com.uzai.console.dto.pyq.pyqautoconfig.PyqAutoConfigDto;
import com.uzai.console.dto.pyq.pyqautoconfig.PyqAutoConfigIdDto;
import com.uzai.console.dto.pyq.pyqautoconfig.PyqAutoConfigQuery;
import com.uzai.console.dto.pyq.pyqautoconfig.PyqAutoConfigStatusDto;
import com.uzai.console.dto.pyq.pyqmaterial.TurnlinkDto;
import com.uzai.console.dto.tb.tbtoken.TbTokenIsSyncOrderDto;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.feign.UzaiTurnlinkFeignService;
import com.uzai.console.service.pyq.PyqAutoConfigService;
import com.uzai.console.vo.feign.turnlink.SimpleTurnlinkVo;
import com.uzai.console.vo.pyq.pyqautoconfig.PyqAutoConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 朋友圈自动发圈配置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class PyqAutoConfigServiceImpl implements PyqAutoConfigService {

    Logger logger = LoggerFactory.getLogger(PyqAutoConfigServiceImpl.class);

    @Autowired
    private PyqAutoConfigMapper pyqAutoConfigMapper;
    @Autowired
    private PyqDevicePlanMapper pyqDevicePlanMapper;

    /**
     * 查询朋友圈自动发圈配置列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigQuery
     * @return null
     */
    public Page<PyqAutoConfigVo> findPyqAutoConfigList(PyqAutoConfigQuery pyqAutoConfigQuery){
        //新建返回到页面的LIST对象
        List<PyqAutoConfigVo> pyqAutoConfigPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<PyqAutoConfig> page = new Page<>();
        pyqAutoConfigQuery.convert(page);
        ArrayList<PyqAutoConfig> pyqAutoConfigList =  pyqAutoConfigMapper.findByList(page, pyqAutoConfigQuery);
        if(pyqAutoConfigList != null && pyqAutoConfigList.size() > 0){
            for (PyqAutoConfig pyqAutoConfig : pyqAutoConfigList){
                PyqAutoConfigVo pyqAutoConfigPageVo = new PyqAutoConfigVo();
                BeanUtils.copyProperties(pyqAutoConfig, pyqAutoConfigPageVo);

                //设备方案名称
                if(pyqAutoConfig.getDevicePlanId() != null){
                    PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqAutoConfig.getDevicePlanId(), pyqAutoConfig.getMerId());
                    if(pyqDevicePlan != null){
                        pyqAutoConfigPageVo.setDevicePlanName(pyqDevicePlan.getName());
                    }
                }

                //封装关键词过滤
                if(StringUtils.isNotBlank(pyqAutoConfig.getIgnore())){
                    List<String> ignoreList = JSONObject.parseArray(pyqAutoConfig.getIgnore(), String.class);
                    pyqAutoConfigPageVo.setIgnoreList(ignoreList);
                }

                //封装图片
                if(StringUtils.isNotBlank(pyqAutoConfig.getImgs())){
                    List<String> imgList = JSONObject.parseArray(pyqAutoConfig.getImgs(), String.class);
                    pyqAutoConfigPageVo.setImgList(imgList);
                }

                //发送商品评论层数列表
                if(StringUtils.isNotBlank(pyqAutoConfig.getGoodsCommentLevel())){
                    List<Integer> goodsCommentLevelList = JSONObject.parseArray(pyqAutoConfig.getGoodsCommentLevel(), Integer.class);
                    pyqAutoConfigPageVo.setGoodsCommentLevelList(goodsCommentLevelList);
                }

                //发送附件类型
                if(StringUtils.isNotBlank(pyqAutoConfig.getMaterialType())){
                    List<Integer> materialTypeList = JSONObject.parseArray(pyqAutoConfig.getMaterialType(), Integer.class);
                    pyqAutoConfigPageVo.setMaterialTypeList(materialTypeList);
                }

                pyqAutoConfigPageVoList.add(pyqAutoConfigPageVo);
            }
        }
        //返回到页面分页对象
        Page<PyqAutoConfigVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(pyqAutoConfigPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询朋友圈自动发圈配置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigIdDto
     * @return null
     */
    public PyqAutoConfigVo findPyqAutoConfig(PyqAutoConfigIdDto pyqAutoConfigIdDto){
        if(pyqAutoConfigIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        PyqAutoConfig pyqAutoConfig = pyqAutoConfigMapper.selectById(pyqAutoConfigIdDto.getId(), pyqAutoConfigIdDto.getMerId());

        if(pyqAutoConfig == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该朋友圈自动发圈配置不存在");
        }

        PyqAutoConfigVo pyqAutoConfigVo = new PyqAutoConfigVo();
        BeanUtils.copyProperties(pyqAutoConfig, pyqAutoConfigVo);

        //封装关键词过滤
        if(StringUtils.isNotBlank(pyqAutoConfig.getIgnore())){
            List<String> ignoreList = JSONObject.parseArray(pyqAutoConfig.getIgnore(), String.class);
            pyqAutoConfigVo.setIgnoreList(ignoreList);
        }

        //封装图片
        if(StringUtils.isNotBlank(pyqAutoConfig.getImgs())){
            List<String> imgList = JSONObject.parseArray(pyqAutoConfig.getImgs(), String.class);
            pyqAutoConfigVo.setImgList(imgList);
        }

        //发送商品评论层数列表
        if(StringUtils.isNotBlank(pyqAutoConfig.getGoodsCommentLevel())){
            List<Integer> goodsCommentLevelList = JSONObject.parseArray(pyqAutoConfig.getGoodsCommentLevel(), Integer.class);
            pyqAutoConfigVo.setGoodsCommentLevelList(goodsCommentLevelList);
        }

        //发送附件类型
        if(StringUtils.isNotBlank(pyqAutoConfig.getMaterialType())){
            List<Integer> materialTypeList = JSONObject.parseArray(pyqAutoConfig.getMaterialType(), Integer.class);
            pyqAutoConfigVo.setMaterialTypeList(materialTypeList);
        }

        return pyqAutoConfigVo;
    }

    /**
     * 添加朋友圈自动发圈配置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigDto
     * @return null
     */
    public void addPyqAutoConfig(PyqAutoConfigDto pyqAutoConfigDto){

        if(pyqAutoConfigDto.getDevicePlanId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqAutoConfigDto.getDevicePlanId(), pyqAutoConfigDto.getMerId());
        if(pyqDevicePlan == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "设备方案不存在");
        }

        if(pyqAutoConfigDto.getType() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择类型");
        }

        if(pyqAutoConfigDto.getMaterialTypeList() == null || pyqAutoConfigDto.getMaterialTypeList().size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择朋友圈素材类型");
        }

        //图文
        if(Tools.getInteger(pyqAutoConfigDto.getSendCycle()).intValue() != 1){
            Integer rateMin = pyqAutoConfigDto.getRateMin();
            Integer rateMax = pyqAutoConfigDto.getRateMax();

            if(rateMin == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最小值");
            }

            if(rateMin.intValue() < 1){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率区间不能少于1");
            }

            if(rateMax == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最大值");
            }

            if(rateMax.intValue() < rateMin.intValue()){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率值范围设置不正确");
            }
        }

        Integer now = DateUtil.getNowTime();
        PyqAutoConfig pyqAutoConfig = new PyqAutoConfig();
        pyqAutoConfig.setId(IdWorker.getId());
        pyqAutoConfig.setMerId(pyqAutoConfigDto.getMerId());
        pyqAutoConfig.setName(pyqAutoConfigDto.getName());
        pyqAutoConfig.setType(pyqAutoConfigDto.getType());
        pyqAutoConfig.setDevicePlanId(pyqAutoConfigDto.getDevicePlanId());
        pyqAutoConfig.setContentId(pyqAutoConfigDto.getContentId());
        pyqAutoConfig.setContent(pyqAutoConfigDto.getContent());
        pyqAutoConfig.setImgs(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getImgList())));
        pyqAutoConfig.setCommentId(pyqAutoConfigDto.getCommentId());
        pyqAutoConfig.setCommentContent(pyqAutoConfigDto.getCommentContent());
        pyqAutoConfig.setLimitTimeStart(pyqAutoConfigDto.getLimitTimeStart());
        pyqAutoConfig.setLimitTimeEnd(pyqAutoConfigDto.getLimitTimeEnd());
        pyqAutoConfig.setRateMin(pyqAutoConfigDto.getRateMin());
        pyqAutoConfig.setRateMax(pyqAutoConfigDto.getRateMax());
        pyqAutoConfig.setSendCycle(Tools.getInteger(pyqAutoConfigDto.getSendCycle()));
        pyqAutoConfig.setLimitRepeatDay(pyqAutoConfigDto.getLimitRepeatDay());
        pyqAutoConfig.setStatus(Tools.getInteger(pyqAutoConfigDto.getStatus()));
        pyqAutoConfig.setIgnore(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getIgnoreList())));
        pyqAutoConfig.setImgSecret(Tools.getInteger(pyqAutoConfigDto.getImgSecret()));
        pyqAutoConfig.setEmoji(Tools.getInteger(pyqAutoConfigDto.getEmoji()));
        pyqAutoConfig.setRandomImg(Tools.getInteger(pyqAutoConfigDto.getRandomImg()));
        pyqAutoConfig.setGoodsComment(Tools.getInteger(pyqAutoConfigDto.getGoodsComment()));
        pyqAutoConfig.setReplaceOld(pyqAutoConfigDto.getReplaceOld());
        pyqAutoConfig.setReplaceNew(pyqAutoConfigDto.getReplaceNew());
        pyqAutoConfig.setGoodsCommentLevel(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getGoodsCommentLevelList())));
        pyqAutoConfig.setMaterialType(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getMaterialTypeList())));
        pyqAutoConfig.setCreateTime(now);
        pyqAutoConfig.setUpdateTime(now);
        pyqAutoConfigMapper.insert(pyqAutoConfig);
    }

    /**
     * 修改朋友圈自动发圈配置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigDto
     * @return null
     */
    public void updatePyqAutoConfig(PyqAutoConfigDto pyqAutoConfigDto){
        if(pyqAutoConfigDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(pyqAutoConfigDto.getDevicePlanId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        if(pyqAutoConfigDto.getType() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择类型");
        }

        if(pyqAutoConfigDto.getMaterialTypeList() == null || pyqAutoConfigDto.getMaterialTypeList().size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择朋友圈素材类型");
        }

        PyqAutoConfig pyqAutoConfig = pyqAutoConfigMapper.selectById(pyqAutoConfigDto.getId(), pyqAutoConfigDto.getMerId());
        if(pyqAutoConfig == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自动发圈配置不存在");
        }

        //图文
        if(Tools.getInteger(pyqAutoConfigDto.getSendCycle()).intValue() != 1){
            Integer rateMin = pyqAutoConfigDto.getRateMin();
            Integer rateMax = pyqAutoConfigDto.getRateMax();

            if(rateMin == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最小值");
            }

            if(rateMin.intValue() < 1){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率区间不能少于1");
            }

            if(rateMax == null){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最大值");
            }

            if(rateMax.intValue() < rateMin.intValue()){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率值范围设置不正确");
            }
        }

        pyqAutoConfig.setMerId(pyqAutoConfigDto.getMerId());
        pyqAutoConfig.setName(pyqAutoConfigDto.getName());
        pyqAutoConfig.setType(pyqAutoConfigDto.getType());
        pyqAutoConfig.setDevicePlanId(pyqAutoConfigDto.getDevicePlanId());
        pyqAutoConfig.setContentId(pyqAutoConfigDto.getContentId());
        pyqAutoConfig.setContent(pyqAutoConfigDto.getContent());
        pyqAutoConfig.setImgs(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getImgList())));
        pyqAutoConfig.setCommentId(pyqAutoConfigDto.getCommentId());
        pyqAutoConfig.setCommentContent(pyqAutoConfigDto.getCommentContent());
        pyqAutoConfig.setLimitTimeStart(pyqAutoConfigDto.getLimitTimeStart());
        pyqAutoConfig.setLimitTimeEnd(pyqAutoConfigDto.getLimitTimeEnd());
        pyqAutoConfig.setRateMin(pyqAutoConfigDto.getRateMin());
        pyqAutoConfig.setRateMax(pyqAutoConfigDto.getRateMax());
        pyqAutoConfig.setSendCycle(Tools.getInteger(pyqAutoConfigDto.getSendCycle()));
        pyqAutoConfig.setLimitRepeatDay(pyqAutoConfigDto.getLimitRepeatDay());
        pyqAutoConfig.setStatus(Tools.getInteger(pyqAutoConfigDto.getStatus()));
        pyqAutoConfig.setIgnore(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getIgnoreList())));
        pyqAutoConfig.setImgSecret(Tools.getInteger(pyqAutoConfigDto.getImgSecret()));
        pyqAutoConfig.setEmoji(Tools.getInteger(pyqAutoConfigDto.getEmoji()));
        pyqAutoConfig.setRandomImg(Tools.getInteger(pyqAutoConfigDto.getRandomImg()));
        pyqAutoConfig.setGoodsComment(Tools.getInteger(pyqAutoConfigDto.getGoodsComment()));
        pyqAutoConfig.setReplaceOld(pyqAutoConfigDto.getReplaceOld());
        pyqAutoConfig.setReplaceNew(pyqAutoConfigDto.getReplaceNew());
        pyqAutoConfig.setGoodsCommentLevel(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getGoodsCommentLevelList())));
        pyqAutoConfig.setMaterialType(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getMaterialTypeList())));
        Integer now = DateUtil.getNowTime();
        pyqAutoConfig.setUpdateTime(now);
        pyqAutoConfigMapper.updateById(pyqAutoConfig);
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigIdDto
     * @return null
     */
    public void deletePyqAutoConfig(PyqAutoConfigIdDto pyqAutoConfigIdDto){
        if(pyqAutoConfigIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }
        pyqAutoConfigMapper.deleteById(pyqAutoConfigIdDto.getId(), pyqAutoConfigIdDto.getMerId());
    }

    /**
     * 设置是否开启
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigStatusDto
     * @return null
     */
    public void updateStatus(PyqAutoConfigStatusDto pyqAutoConfigStatusDto){
        if(pyqAutoConfigStatusDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        PyqAutoConfig pyqAutoConfig = pyqAutoConfigMapper.selectById(pyqAutoConfigStatusDto.getId(), pyqAutoConfigStatusDto.getMerId());

        if(pyqAutoConfig == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此自动发圈配置不存在");
        }

        int now = DateUtil.getNowTime();

        if(pyqAutoConfig.getType().intValue() == 10){ //悠友圈商品，启动时将现住时间修改成发送时间
            pyqAutoConfig.setStatus(Tools.getInteger(pyqAutoConfigStatusDto.getStatus()));
            pyqAutoConfig.setUpdateTime(now);
            //修改状态
            pyqAutoConfigMapper.updateStatusById(pyqAutoConfig);
            //修改发送时间
            pyqAutoConfig.setSendTime(now);
            pyqAutoConfigMapper.updateSendTimeById(pyqAutoConfig);
        }else{
            pyqAutoConfig.setStatus(Tools.getInteger(pyqAutoConfigStatusDto.getStatus()));
            pyqAutoConfig.setUpdateTime(now);
            //修改状态
            pyqAutoConfigMapper.updateStatusById(pyqAutoConfig);
        }

        //AOP切面，封装操作日志内容
        String aspectOperatorLog = "修改朋友圈自动发送任务状态。任务名称:"+pyqAutoConfig.getName();
        if(Tools.getInteger(pyqAutoConfigStatusDto.getStatus()) == 1){
            aspectOperatorLog = "开启朋友圈自动发送任务状态。任务名称:"+pyqAutoConfig.getName();
        }else if(Tools.getInteger(pyqAutoConfigStatusDto.getStatus()) == 0){
            aspectOperatorLog = "关闭朋友圈自动发送任务状态。任务名称:"+pyqAutoConfig.getName();
        }
        pyqAutoConfigStatusDto.setAspectOperatorLog(aspectOperatorLog);

    }

    /**
     * 添加优友圈朋友圈自动发圈配置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigDto
     * @return null
     */
    public void addPyqGoodsUserAutoConfig(PyqAutoConfigDto pyqAutoConfigDto){
        if(pyqAutoConfigDto.getDevicePlanId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        PyqDevicePlan pyqDevicePlan = pyqDevicePlanMapper.selectById(pyqAutoConfigDto.getDevicePlanId(), pyqAutoConfigDto.getMerId());
        if(pyqDevicePlan == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "设备方案不存在");
        }

        if(pyqAutoConfigDto.getYzjxGoodsUserId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择优友圈id");
        }

        if(pyqAutoConfigDto.getType() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择类型");
        }

        Integer rateMin = pyqAutoConfigDto.getRateMin();
        Integer rateMax = pyqAutoConfigDto.getRateMax();

        if(rateMin == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最小值");
        }

        if(rateMin.intValue() < 1){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率区间不能少于1");
        }

        if(rateMax == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最大值");
        }

        if(rateMax.intValue() < rateMin.intValue()){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率值范围设置不正确");
        }

        Integer now = DateUtil.getNowTime();
        PyqAutoConfig pyqAutoConfig = new PyqAutoConfig();
        pyqAutoConfig.setId(IdWorker.getId());
        pyqAutoConfig.setMerId(pyqAutoConfigDto.getMerId());
        pyqAutoConfig.setName(pyqAutoConfigDto.getName());
        pyqAutoConfig.setType(pyqAutoConfigDto.getType());
        pyqAutoConfig.setDevicePlanId(pyqAutoConfigDto.getDevicePlanId());
        pyqAutoConfig.setContentId(pyqAutoConfigDto.getContentId());
        pyqAutoConfig.setContent(pyqAutoConfigDto.getContent());
        pyqAutoConfig.setImgs(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getImgList())));
        pyqAutoConfig.setCommentId(pyqAutoConfigDto.getCommentId());
        pyqAutoConfig.setCommentContent(pyqAutoConfigDto.getCommentContent());
        pyqAutoConfig.setLimitTimeStart(pyqAutoConfigDto.getLimitTimeStart());
        pyqAutoConfig.setLimitTimeEnd(pyqAutoConfigDto.getLimitTimeEnd());
        pyqAutoConfig.setRateMin(pyqAutoConfigDto.getRateMin());
        pyqAutoConfig.setRateMax(pyqAutoConfigDto.getRateMax());
        pyqAutoConfig.setSendCycle(Tools.getInteger(pyqAutoConfigDto.getSendCycle()));
        pyqAutoConfig.setLimitRepeatDay(pyqAutoConfigDto.getLimitRepeatDay());
        pyqAutoConfig.setStatus(Tools.getInteger(pyqAutoConfigDto.getStatus()));
        pyqAutoConfig.setIgnore(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getIgnoreList())));
        pyqAutoConfig.setImgSecret(Tools.getInteger(pyqAutoConfigDto.getImgSecret()));
        pyqAutoConfig.setEmoji(Tools.getInteger(pyqAutoConfigDto.getEmoji()));
        pyqAutoConfig.setRandomImg(Tools.getInteger(pyqAutoConfigDto.getRandomImg()));
        pyqAutoConfig.setGoodsComment(Tools.getInteger(pyqAutoConfigDto.getGoodsComment()));
        pyqAutoConfig.setReplaceOld(pyqAutoConfigDto.getReplaceOld());
        pyqAutoConfig.setReplaceNew(pyqAutoConfigDto.getReplaceNew());
        pyqAutoConfig.setGoodsCommentLevel(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getGoodsCommentLevelList())));
        pyqAutoConfig.setYzjxGoodsUserId(pyqAutoConfigDto.getYzjxGoodsUserId());
        pyqAutoConfig.setMaterialType(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getMaterialTypeList())));
        pyqAutoConfig.setCreateTime(now);
        pyqAutoConfig.setUpdateTime(now);
        pyqAutoConfigMapper.insert(pyqAutoConfig);
    }

    /**
     * 修改朋友圈优友圈自动发圈配置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqAutoConfigDto
     * @return null
     */
    public void updatePyqGoodsUserAutoConfig(PyqAutoConfigDto pyqAutoConfigDto){
        if(pyqAutoConfigDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(pyqAutoConfigDto.getDevicePlanId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择设备方案");
        }

        if(pyqAutoConfigDto.getYzjxGoodsUserId() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择优友圈id");
        }

        if(pyqAutoConfigDto.getType() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择类型");
        }

        PyqAutoConfig pyqAutoConfig = pyqAutoConfigMapper.selectById(pyqAutoConfigDto.getId(), pyqAutoConfigDto.getMerId());
        if(pyqAutoConfig == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该自动发圈配置不存在");
        }

        Integer rateMin = pyqAutoConfigDto.getRateMin();
        Integer rateMax = pyqAutoConfigDto.getRateMax();

        if(rateMin == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最小值");
        }

        if(rateMin.intValue() < 1){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率区间不能少于1");
        }

        if(rateMax == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入发送频率最大值");
        }

        if(rateMax.intValue() < rateMin.intValue()){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "发送频率值范围设置不正确");
        }

        pyqAutoConfig.setMerId(pyqAutoConfigDto.getMerId());
        pyqAutoConfig.setName(pyqAutoConfigDto.getName());
        pyqAutoConfig.setType(pyqAutoConfigDto.getType());
        pyqAutoConfig.setDevicePlanId(pyqAutoConfigDto.getDevicePlanId());
        pyqAutoConfig.setContentId(pyqAutoConfigDto.getContentId());
        pyqAutoConfig.setContent(pyqAutoConfigDto.getContent());
        pyqAutoConfig.setImgs(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getImgList())));
        pyqAutoConfig.setCommentId(pyqAutoConfigDto.getCommentId());
        pyqAutoConfig.setCommentContent(pyqAutoConfigDto.getCommentContent());
        pyqAutoConfig.setLimitTimeStart(pyqAutoConfigDto.getLimitTimeStart());
        pyqAutoConfig.setLimitTimeEnd(pyqAutoConfigDto.getLimitTimeEnd());
        pyqAutoConfig.setRateMin(pyqAutoConfigDto.getRateMin());
        pyqAutoConfig.setRateMax(pyqAutoConfigDto.getRateMax());
        pyqAutoConfig.setSendCycle(Tools.getInteger(pyqAutoConfigDto.getSendCycle()));
        pyqAutoConfig.setLimitRepeatDay(pyqAutoConfigDto.getLimitRepeatDay());
        pyqAutoConfig.setStatus(Tools.getInteger(pyqAutoConfigDto.getStatus()));
        pyqAutoConfig.setIgnore(Tools.getStrEmpty(JSONObject.toJSONString(pyqAutoConfigDto.getIgnoreList())));
        pyqAutoConfig.setImgSecret(Tools.getInteger(pyqAutoConfigDto.getImgSecret()));
        pyqAutoConfig.setEmoji(Tools.getInteger(pyqAutoConfigDto.getEmoji()));
        pyqAutoConfig.setRandomImg(Tools.getInteger(pyqAutoConfigDto.getRandomImg()));
        pyqAutoConfig.setGoodsComment(Tools.getInteger(pyqAutoConfigDto.getGoodsComment()));
        pyqAutoConfig.setReplaceOld(pyqAutoConfigDto.getReplaceOld());
        pyqAutoConfig.setReplaceNew(pyqAutoConfigDto.getReplaceNew());
        pyqAutoConfig.setGoodsCommentLevel(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getGoodsCommentLevelList())));
        pyqAutoConfig.setYzjxGoodsUserId(pyqAutoConfigDto.getYzjxGoodsUserId());
        pyqAutoConfig.setMaterialType(Tools.getStrNull(JSONObject.toJSONString(pyqAutoConfigDto.getMaterialTypeList())));
        Integer now = DateUtil.getNowTime();
        pyqAutoConfig.setUpdateTime(now);
        pyqAutoConfigMapper.updateById(pyqAutoConfig);
    }
}
